﻿using System;
using System.Drawing;
using System.Windows.Forms;

using Spencen.Mobile.UI.Primitives;

namespace Spencen.Mobile.UI.Controls
{
    public class FormHost : DrawingContainer
    {
        private Control _hostControl;

        public FormHost( IDrawingHost host ) : base( host )
        {
            _hostControl = host as Control;
            if ( _hostControl == null )
                throw new ArgumentException("FormHost must have an IDrawingHost that is a Windows.Forms.Control.", "host");

            // By default we have no background or border
            Background = new SolidBrush( Color.Transparent );
            Stroke = null;
        }

        public Control HostedControl { get; set; }

        public override bool SupportsRotation { get { return false; } }

        protected override void OnRender( GraphicsContext context )
        {
            base.OnRender( context );

            var transformedBounds = TransformedBounds( context );

            if ( !_hostControl.Controls.Contains( HostedControl ) )
                _hostControl.Controls.Add( HostedControl );
            
            var newLocation = new Point( transformedBounds.Left, transformedBounds.Top );
            if ( newLocation != HostedControl.Location )
                HostedControl.Location = newLocation;
            var newSize = new Size( transformedBounds.Width + 1, transformedBounds.Height + 1);
            if ( newSize != HostedControl.Size )
                HostedControl.Size = newSize;
        }
    }
}
